package com.api.browser.service.impl;

import com.alibaba.fastjson.JSON;
import com.api.browser.bean.*;
import com.api.browser.service.BrowserService;
import com.api.browser.util.*;
import com.cloudstore.dev.api.bean.SplitMobileDataBean;
import com.cloudstore.dev.api.bean.SplitMobileTemplateBean;
import org.apache.log4j.Logger;
import weaver.general.Util;
import weaver.systeminfo.SystemEnv;
import weaver.workflow.workflow.WorkflowComInfo;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * 自定义浏览按钮
 * 
 *
 */

public class E8workflowCustomBrow extends BrowserService{
	public static final String JSON_CONFIG = "[    {        \"configs\": [            {                \"configs\": [                    {                        \"key\": \"lastname\"                    }                ],                \"key\": \"col1_row1\"            },            {                \"configs\": [                    {                        \"key\": \"workcode\"                    },                ],                \"key\": \"col1_row2\"            }        ],        \"key\": \"col1\"    }]";
	public Map<String, Object> getBrowserData(Map<String, Object> arg0)
			throws Exception {
		Logger log = Logger.getLogger(Logger.class);
		//String sqlwhere=getWhere(arg0);
		// TODO Auto-generated method stub
		List<SplitTableColBean> cols = new ArrayList<SplitTableColBean>();
		String backfields, fromSql, sqlwhere, orderby;
		sqlwhere=" 1=1 " ;//getWhere(arg0);
		backfields=" * ";//返回字段
		fromSql="  (SELECT * from ("+
				" select distinct workflow_requestbase.requestid ,requestname,creater,creatertype,createdate,createtime,createdate+' '+createtime as createtimes from workflow_requestbase , workflow_currentoperator , workflow_base " +
				" where workflow_currentoperator.requestid = workflow_requestbase.requestid and workflow_currentoperator.userid="+user.getUID()+" and workflow_currentoperator.usertype="+user.getType()+" "+
				" and workflow_requestbase.workflowid = workflow_base.id and (workflow_base.isvalid='1' or workflow_base.isvalid='3') "+
				" ) t1 ) t2 ";//来源表
		//sqlwhere=sqlWhere;//where 条件
		orderby=" requestid ";//排序字段
		Map<String, Object> apidatas=new HashMap<String, Object>();
		System.out.println(fromSql);
		log.error(fromSql);
		log.error(sqlwhere);

		cols.add(new SplitTableColBean("hide", "requestid"));
        cols.add(new SplitTableColBean("50%", SystemEnv.getHtmlLabelName(1268, user.getLanguage()), "requestname", "requestname", 1).setIsInputCol(BoolAttr.TRUE));
        cols.add(new SplitTableColBean("20%", SystemEnv.getHtmlLabelName(63, user.getLanguage()), "creater", "creater", 2));
        cols.add(new SplitTableColBean("30%", SystemEnv.getHtmlLabelName(1278, user.getLanguage()), "createtimes", "createtimes",  3));
        SplitTableBean tableBean = new SplitTableBean(backfields, fromSql, sqlwhere, orderby, "requestid", cols);
		tableBean.setDatasource("com.api.browser.service.impl.E8DataQuery.getScheduleList");

        apidatas.putAll(SplitTableUtil.makeListDataResult(tableBean));

        tableBean.setMobileshowtype(MobileShowTypeAttr.ListView);
        try
        {
          SplitMobileTemplateBean localSplitMobileTemplateBean = new SplitMobileTemplateBean();
          localSplitMobileTemplateBean.addJsonTemplate("json", JSON.parseArray("[    {        \"configs\": [            {                \"configs\": [                    {                        \"key\": \"lastname\"                    }                ],                \"key\": \"col1_row1\"            },            {                \"configs\": [                    {                        \"key\": \"workcode\"                    },                ],                \"key\": \"col1_row2\"            }        ],        \"key\": \"col1\"    }]", SplitMobileDataBean.class));
          tableBean.createMobileTemplate(localSplitMobileTemplateBean);
        }
        catch (Exception localException)
        {
          log.error("localException--->"+localException.getMessage());
          localException.printStackTrace();
        }
        log.error("apidatas-E8workflowCustomBrow-->"+apidatas.toString());
        return apidatas;
	}
	@Override
	public Map<String, Object> getBrowserConditionInfo(Map<String, Object> arg0)
		throws Exception {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		Logger log = Logger.getLogger(Logger.class);
		try{
		
	// TODO Auto-generated method stub
		
		List<SearchConditionItem> conditions = new ArrayList<SearchConditionItem>();
		//User user = (User) arg0.get("user");
		ConditionFactory conditionFactory = new ConditionFactory(this.user);
		conditions.add(conditionFactory.createCondition(ConditionType.INPUT, 388770, "lastname"));//请求标题
		conditions.add(conditionFactory.createCondition(ConditionType.INPUT, 126847, "requestmark"));//流程编号
		BrowserBean browserBean=new BrowserBean();
		browserBean.setType("17");
		conditions.add(conditionFactory.createCondition(ConditionType.BROWSER,502996,"creater",browserBean));//创建人
		//创建人部门
			BrowserBean browserBeanDep=new BrowserBean();
			browserBeanDep.setType("4");
			conditions.add(conditionFactory.createCondition(ConditionType.BROWSER,130043,"department",browserBeanDep));//创建人部门
		//创建人分部 130042
			BrowserBean browserBeanSub=new BrowserBean();
			browserBeanSub.setType("164");
			conditions.add(conditionFactory.createCondition(ConditionType.BROWSER,130042,"subid",browserBeanSub));//创建人分部
		//创建日期
			conditions.add(conditionFactory.createCondition(ConditionType.DATE,"日期1","createdatestart"));
			conditions.add(conditionFactory.createCondition(ConditionType.DATE,"日期2","createdateend"));
		//接收期间
			List<SearchConditionOption> statuOptions = new ArrayList<SearchConditionOption>();
			statuOptions.add(new SearchConditionOption("",""));
			statuOptions.add(new SearchConditionOption("6","最近6个月"));
			statuOptions.add(new SearchConditionOption("12","最近12个月"));
			statuOptions.add(new SearchConditionOption("18","最近18个月"));
			statuOptions.add(new SearchConditionOption("36","全部"));
			conditions.add(conditionFactory.createCondition(ConditionType.SELECT, 31787, "date2during", statuOptions));
		//conditions.add(conditionFactory.createCondition(ConditionType.BROWSER, 63, "type"));
		//conditions.add(conditionFactory.createCondition(ConditionType.BROWSER, 602, "customerStatus", "cusstatus", 15078));

		//List<SearchConditionOption> statuOptions = new ArrayList<SearchConditionOption>();
		//statuOptions.add(new SearchConditionOption("",""));
		//statuOptions.add(new SearchConditionOption("1",SystemEnv.getHtmlLabelName(23773, user.getLanguage())));
		//statuOptions.add(new SearchConditionOption("2",SystemEnv.getHtmlLabelName(23774, user.getLanguage())));
		//conditions.add(conditionFactory.createCondition(ConditionType.SELECT, 19061, "status", statuOptions));

		apidatas.put("conditions", conditions);
		log.error("apidatas--->"+apidatas.toString());
		}catch(Exception e){
			log.error("getBrowserConditionInfo--->"+apidatas.toString());
		}
		return apidatas;
	
	}

	/*
	获取 where 条件
	 */
	public String getWhere(Map<String, Object> arg0){
		String sqlwhere =" where 1=1 ";
		int date2during = Util.getIntValue((String)arg0.get("date2during"), -1);
		String issearch = Util.null2String(arg0.get("issearch"));
		String requestname = Util.null2String(arg0.get("requestname"));
		String creater = Util.null2String(arg0.get("creater"));
		String createdatestart = Util.null2String(arg0.get("createdatestart"));
		String createdateend = Util.null2String(arg0.get("createdateend"));
		String isrequest = Util.null2String(arg0.get("isrequest"));
		String requestmark = Util.null2String(arg0.get("requestmark"));
		String prjids = Util.null2String(arg0.get("prjids"));
		String crmids = Util.null2String(arg0.get("crmids"));
		String workflowid = Util.null2String(arg0.get("workflowid"));
		String department = Util.null2String(arg0.get("department"));
		String status = Util.null2String(arg0.get("status"));
		String subid = Util.null2String(arg0.get("subid"));
		//当前人员待定
		if (!"".equals(requestname)) {
			sqlwhere += " and requestnamenew like '%" + Util.fromScreen2(requestname,user.getLanguage()) + "%'";
		}
		if(!"".equals(workflowid) && !"0".equals(workflowid)){
			sqlwhere += " and workflow_requestbase.workflowid in (" + WorkflowVersion.getAllVersionStringByWFIDs(workflowid) + ")";
		}
		if (!"".equals(requestmark)) {
			sqlwhere += " and requestmark like '%" + requestmark +"%'";
		}
		if (!"".equals(creater)) {
			sqlwhere += " and creater in(" + creater + ") and creatertype=0 " ;
		}
		if (!"".equals(department) && !"0".equals(department)) {
			sqlwhere += " and workflow_requestbase.creater in (select id from hrmresource where departmentid in (" + department + "))";
		}
		if (!"".equals(subid) && !"0".equals(subid)) {
			sqlwhere += " and workflow_requestbase.creater in (select id from hrmresource where subcompanyid1 in (" + subid + "))";
		}
		if (!"".equals(createdatestart)) {
			sqlwhere += " and createdate >='" + createdatestart + "'";
		}
		if (!"".equals(createdateend)) {
			sqlwhere += " and createdate <='" + createdateend + "'";
		}
		if(date2during>0){
			WorkflowComInfo WorkflowComInfo=new WorkflowComInfo();
			sqlwhere += WorkflowComInfo.getDateDuringSql(date2during);
		}
		return sqlwhere;
	}
}
